from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *


# Create your views here.


class IndexView(APIView):

    def get(self, request):
        # 查询出所有的分类

        queryset = Category.objects.all()
        ser = CategorySer(queryset, many=True)
        data_list = []
        for item in queryset:
            # 创建一个 临时字典 存放一个 分类信息
            temp = {}
            # 查询 分类下的所有商品
            goods_queryset = Goods.objects.filter(cate_id=item.id)
            temp["cate"] = CategorySer(item).data
            temp["goods_list"] = GoodsSer(goods_queryset, many=True).data

            # 把一个分类下的所有信息存入到列表
            data_list.append(temp)

        return Response(data_list)
